package com.netposa.datawarehouse.aggregator.listenter;

import com.netposa.datawarehouse.aggregator.util.VehicleAttrContext;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class VehicleAttrInit implements ApplicationListener<ApplicationReadyEvent> {
   private static final Logger log = LoggerFactory.getLogger(VehicleAttrInit.class);

   public void onApplicationEvent(ApplicationReadyEvent event) {
      this.parseAttrXML();
      this.parseBrandXML();
      log.debug("格林深瞳字典属性值:{}", VehicleAttrContext.attrMap);
   }

   private void parseAttrXML() {
      InputStream in = null;

      try {
         in = VehicleAttrContext.class.getResourceAsStream("/vehcileattr/VehicleAttribute.xml");
         SAXReader saxReader = new SAXReader();
         Document doc = saxReader.read(in);
         Element rootElement = doc.getRootElement();
         VehicleAttrContext.AttrEnum[] var5 = VehicleAttrContext.AttrEnum.values();
         int var6 = var5.length;

         for(int var7 = 0; var7 < var6; ++var7) {
            VehicleAttrContext.AttrEnum attrEnum = var5[var7];
            String attrName = attrEnum.name();
            Element element = rootElement.element(attrName);
            if (element != null) {
               List childNode = element.elements();
               Map valAttr = new HashMap();
               Iterator var13 = childNode.iterator();

               while(var13.hasNext()) {
                  Object aChildNode = var13.next();
                  Element next = (Element)aChildNode;
                  Attribute pcc = next.attribute("pcc");
                  Attribute vim = next.attribute("vim");
                  valAttr.put(pcc.getValue(), vim.getValue());
               }

               VehicleAttrContext.attrMap.put(attrEnum, valAttr);
            }
         }
      } catch (Exception var21) {
         log.error(var21.getMessage(), var21);
      } finally {
         IOUtils.closeQuietly(in);
      }

   }

   private void parseBrandXML() {
      InputStream in = null;

      try {
         in = VehicleAttrContext.class.getResourceAsStream("/vehcileattr/VehicleBrand.xml");
         SAXReader saxReader = new SAXReader();
         Document doc = saxReader.read(in);
         Element rootElement = doc.getRootElement();
         List elements = rootElement.elements();
         Map valAttr = new HashMap();
         Iterator var7 = elements.iterator();

         while(var7.hasNext()) {
            Object ele = var7.next();
            Element recodeEle = (Element)ele;
            List childNode = recodeEle.elements();
            String pcc = null;
            String vim = null;
            Iterator var13 = childNode.iterator();

            while(var13.hasNext()) {
               Object aChildNode = var13.next();
               Element next = (Element)aChildNode;
               if (next.getName().equals("深瞳主品牌")) {
                  pcc = next.getTextTrim();
               }

               if (next.getName().equals("KEY_")) {
                  vim = next.getTextTrim();
               }
            }

            valAttr.put(pcc, vim);
         }

         VehicleAttrContext.attrMap.put(VehicleAttrContext.AttrEnum.VehicleBrand, valAttr);
      } catch (Exception var19) {
         log.error(var19.getMessage(), var19);
      } finally {
         IOUtils.closeQuietly(in);
      }

   }
}
